/*
 * Copyright 2008 M-Way Solutions
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mwaysolutions.ocm;


import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;


/**
 *
 * @author <a href="mailto:m.zdila@mwaysolutions.com">Martin Ždila</a>
 */
public interface OcmWriter {

	String COLLECTION_ITEM_NAME = "ocm:item";


	/**
	 * Save value to specified node as jcrItemName
	 * 
	 * @param node parent node where value is to be saved
	 * @param jcrItemName name of the item in the JCR under which the value is to be saved
	 * @param value value to save
	 * @param asReference save value as reference to different node. Value must be referenceable.
	 * @param nonRefAsNew indicates, that if object is not of referenceable type and will be saved as JCR node,
	 * 			then a new node should be created in any case. Otherwise a same-name sibling node is updated with the value.
	 * 			This should be set to <code>false</code> for regular bean properties and <code>true</code> for collection items.
	 * @return item representing saved value
	 * @throws RepositoryException
	 */
	Item saveOrUpdate(Node node, String jcrItemName, Object value, boolean asReference, boolean nonRefAsNew, OcmWriter ocmWriter) throws RepositoryException;

	
	/**
	 * Saves/Updates value to node
	 * 
	 * @param node node directly representing the value
	 * @param value value to save/update
	 * @throws RepositoryException
	 */
	void saveOrUpdate(Node node, Object value, OcmWriter ocmWriter) throws RepositoryException;


	/**
	 * Save delayed node references after all values has been saved.
	 * @throws RepositoryException
	 */
	void saveDelayedReferences() throws RepositoryException;

}
